# encoding: utf-8
__author__ = 'user'

class Dict(dict):

    def __init__(self,**kwargs):
        super(Dict,self).__init__(**kwargs)

    def __getattr__(self, item):
        try:
            return self[item]
        except KeyError:
            raise AttributeError("Dict object has no attribute %s" % item)

    def __setattr__(self, key, value):
        self[key] = value

    def __repr__(self):
        print( self.__name__)

import unittest

class TestDict(unittest.TestCase):
    # 以test开头的方法被当作测试方法
    def test_init(self):
        d = Dict(a=1,b="test")
        self.assertEqual(d.a,1)
        self.assertEqual(d.b,"test")
        self.assertTrue(isinstance(d,dict))

    def test_key(self):
        d = Dict()
        d["key1"] = "value1"
        self.assertEqual(d.key1,"value1")

    def test_attr(self):
        d = Dict()
        d.key1 = "value1"
        self.assertTrue("key1" in d)
        self.assertEqual(d["key1"] ,"value1")

    # with 类似于dispose模式 __enter__  __exit__
    def test_keyerror(self):
        d = Dict()
        with self.assertRaises(KeyError):
            value = d["empty"]

    def test_attrerror(self):
        d = Dict()
        with self.assertRaises(AttributeError):
            value = d.empty

    def setUp(self):
        print "when sets up:",self._testMethodName

    def tearDown(self):
        print "tear down"

if __name__ == "__main__":
    unittest.main()